﻿package 
{
	import flash.events.Event;
	import org.papervision3d.core.effects.view.ReflectionView;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	
	/**
	 * ...
	 * @author qipy
	 */
	public class ReflectionViewEx extends ReflectionView 
	{
		private var container:DisplayObject3D;
		private var sphere:Sphere;
		
		public function ReflectionViewEx():void 
		{
			super(stage.stageWidth, stage.stageHeight, true, true);
			stage.frameRate = 60;
			
			init();
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			camera.z = -100;
		}
		
		private function init():void 
		{
			surfaceHeight = -21;
			
			container = new DisplayObject3D();
			scene.addChild(container);
			
			var numOfPlanes:Number = 9;
			var randius:Number = 45;
			var angle:Number = Math.PI * 2 / numOfPlanes;
			
			var dist:Number = 300;
			
			for (var i:int = 0; i < numOfPlanes; i++) 
			{
				var plane:Plane = new Plane(createBitmapMaterial(i), 20, 20, 2, 2);
				plane.material.doubleSided = true;
				plane.material.smooth = true;
				plane.material.tiled = true;
				scene.addChild(plane);
				plane.x = Math.cos(i * angle) * randius;
				plane.z = Math.sin(i * angle) * randius;
				plane.rotationY = ( -i * angle) * 180 / Math.PI + 270;
				plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onClick);
				container.addChild(plane);
			}
		}
		
		private function createBitmapMaterial(i:uint):BitmapMaterial 
		{
			var random:Number = Math.round(Math.random() * 4);
			var material:BitmapFileMaterial = new BitmapFileMaterial("assets/image_" + i + ".jpg");
			material.interactive = true;
			return material;
		}
		
		private function onClick(e:InteractiveScene3DEvent):void 
		{
			e.displayObject3D.y += .5;
		}
		
		public function onEnterFrame(e:Event):void 
		{
			container.yaw(.5);
			singleRender();
		}
	}
	
}